#include "GUI_Image.h"
#include "GUI_core.h"

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include <stdbool.h>
#include "lcd_ST77903.h"



extern uint8_t displaybuf2[LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT]; //备份缓存区
//整屏幕每一行缓冲
extern uint8_t LCD_Display_Buf[LCD_GUI_HEIGHT][LCD_GUI_WIDTH*2];


uint8_t  Line_Display_Buf[240*2]={0};


extern void LCD_Write_Cmd(uint8_t cmd);

extern void LCD_Write_Data(uint8_t data);






void LCD_Show_Background(uint16_t x,uint16_t y,uint32_t address,uint8_t k)
{
	uint16_t  x1=0,y1=0;
	
			uint32_t  length=0;
			uint32_t Address=0;
			Image_Information_t image;
			uint8_t buf[8]={0};
			qspi_flash_read(address, 8,buf); //读取图片信息
			image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
			image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
			image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
  #if 0   //打印图片信息地址
			printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
  #endif
			//printf("\r\n");
			//printf("\r\n");
			//printf("\r\n");
			//memcpy((Image_Information_t *)image, (uint32_t *)address, 8);
			//printf("address=%x\r\n",address);
			length=image.Width*2;//获取图片大小
	  
			if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
			{
				length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
			}
	  
			//uint8_t buffer[length];
			Address=image.Address;
			//printf("Image Address=%d\r\n",Address);
			//qspi_flash_read(Address,length,display_buf);
	
	
			for(y1=y;y1<y+image.Height;y1++)
			{
			  qspi_flash_read(Address,image.Width*2,&LCD_Display_Buf[y1][x]);
			}
	

    


}




void GUI_Show_Image(uint16_t x,uint16_t y,uint32_t address)
{

         uint8_t i=0;
         uint16_t  x1=0,y1=0;

		  uint32_t	length=0;
		  uint32_t Address=0;
		  Image_Information_t image;
		  uint8_t buf[8]={0};
		  qspi_flash_read(address, 8,buf); //读取图片信息
		  image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
		  image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
		  image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
	#if 1   //打印图片信息地址
		  printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
	#endif
		  //printf("\r\n");
		  //printf("\r\n");
		  //printf("\r\n");
		  //memcpy((Image_Information_t *)image, (uint32_t *)address, 8);
		  //printf("address=%x\r\n",address);
		  length=image.Width*2;//获取图片大小
	
		  if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
		  {
			  length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
		  }
	
		  //uint8_t buffer[length];
		  Address=image.Address;
		  //printf("Image Address=%d\r\n",Address);
		  //qspi_flash_read(Address,length,display_buf);

		  memcpy(LCD_Display_Buf,displaybuf2,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1


          for(y1=y,i=0;y1<y+image.Height;y1++,i++)
          {
		  	qspi_flash_read(Address+i*image.Width*2,image.Width*2,&LCD_Display_Buf[y1][x*2]);
		  }


          memcpy(displaybuf2,LCD_Display_Buf,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1

         










		  
		  //更新显示缓冲区  备份缓冲区
		  // memcpy(displaybuf1,display_buf,LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT); //将显示缓冲区2更新到主缓冲区1
}






void GUI_Show_Image_Num(int x,int y,uint32_t address,uint8_t num)
{
 //printf("--------------%s-----------------\r\n",__FUNCTION__);
         uint8_t i=0;
         int  x1=0,y1=0;

		  uint32_t	length=0;
		  uint32_t Address=0;
		  Image_Information_t image;
		  uint8_t buf[8]={0};
		  qspi_flash_read(address, 8,buf); //读取图片信息
		  image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
		  image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
		  image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
	#if 0   //打印图片信息地址
		  printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
	#endif
		  //printf("\r\n");
		  //printf("\r\n");
		  //printf("\r\n");
		  //memcpy((Image_Information_t *)image, (uint32_t *)address, 8);
		  //printf("address=%x\r\n",address);
		  length=image.Width*2;//获取图片大小
	
		  if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
		  {
			  length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
		  }
	
		
		  Address=image.Address;
		  //printf("Image Address=%d\r\n",Address);
		  //qspi_flash_read(Address,length,display_buf);

		  memcpy(LCD_Display_Buf,displaybuf2,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1


          for(y1=y,i=0;y1<y+image.Height/10;y1++,i++)
          {

			qspi_flash_read(Address+num*image.Width*image.Height*2/10  + i*image.Width*2,image.Width*2,Line_Display_Buf);

			


            for(int j=0;j<image.Width*2;j++)
            {
            	//printf("%d\r\n",j);
                //判断当前是否为纯黑

                  if(Line_Display_Buf[j]==0x00)
                  {
                	  //printf("Line_Display_Buf[j]==0x00\r\n");
                     //break;
				  }else
				  {
					  //printf("LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j]\r\n");
				       LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j];
				  }
           
				
			}
		  
		  	//qspi_flash_read(Address+num*image.Width*image.Height*2/10  + i*image.Width*2,image.Width*2,&LCD_Display_Buf[y1][x*2]);
		  }


          memcpy(displaybuf2,LCD_Display_Buf,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1

         










		  
		  //更新显示缓冲区  备份缓冲区
		  // memcpy(displaybuf1,display_buf,LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT); //将显示缓冲区2更新到主缓冲区1
}






void GUI_Show_Image_Num_Offset(int x,int y,uint32_t address,uint8_t num)
{




 //printf("--------------%s-----------------\r\n",__FUNCTION__);
         uint8_t i=0;
         int  x1=0,y1=0;
         int y2=0;//记录当前y相差的偏移

		  uint32_t	length=0;
		  uint32_t Address=0;
		  Image_Information_t image;
		  uint8_t buf[8]={0};
		  qspi_flash_read(address, 8,buf); //读取图片信息
		  image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
		  image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
		  image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址

if(y<  - image.Height ||y>LCD_GUI_HEIGHT)
{
    return ;
}



 if(x<  - image.Width ||x>LCD_GUI_WIDTH)
 {
			  return ;
 }




	#if 0   //打印图片信息地址
		  printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
	#endif
		  //printf("\r\n");
		  //printf("\r\n");
		  //printf("\r\n");
		  //memcpy((Image_Information_t *)image, (uint32_t *)address, 8);
		  //printf("address=%x\r\n",address);
		  length=image.Width*2;//获取图片大小
	
		  if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
		  {
			  length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
		  }
	
		
		  Address=image.Address;
		  //printf("Image Address=%d\r\n",Address);
		  //qspi_flash_read(Address,length,display_buf);

		  memcpy(LCD_Display_Buf,displaybuf2,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1

	     if(y<0)
	     {
          i=abs(y);
          y2=i;
	     }else if( (y< (LCD_GUI_HEIGHT - image.Height/10))  && (y> 0) )
	     {
		   i=0;
		   y2=0;
		 }
	     else if( y > LCD_GUI_HEIGHT - image.Height/10)
	     {
		   i=0;
		   y2=  image.Height/10 - (LCD_GUI_HEIGHT-  y)   ;
		 }
          for(y1=y;y1<y+image.Height/10 - y2;y1++,i++)
          {

			qspi_flash_read(Address+num*image.Width*image.Height*2/10  + i*image.Width*2,image.Width*2,Line_Display_Buf);

			


            for(int j=0;j<image.Width*2;j++)
            {
            	//printf("%d\r\n",j);
                //判断当前是否为纯黑

                  if(Line_Display_Buf[j]==0x00)
                  {
                	  //printf("Line_Display_Buf[j]==0x00\r\n");
                     //break;
				  }else
				  {
					  //printf("LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j]\r\n");
				       LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j];
				  }
           
				
			}
		  
		  	//qspi_flash_read(Address+num*image.Width*image.Height*2/10  + i*image.Width*2,image.Width*2,&LCD_Display_Buf[y1][x*2]);
		  }


          memcpy(displaybuf2,LCD_Display_Buf,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1

         










		  
		  //更新显示缓冲区  备份缓冲区
		  // memcpy(displaybuf1,display_buf,LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT); //将显示缓冲区2更新到主缓冲区1
}





void GUI_Show_Image_Week(uint16_t x,uint16_t y,uint32_t address,uint8_t num)
{
 //printf("--------------%s-----------------\r\n",__FUNCTION__);
         uint8_t i=0;
         uint16_t  x1=0,y1=0;

		  uint32_t	length=0;
		  uint32_t Address=0;
		  Image_Information_t image;
		  uint8_t buf[8]={0};
		  qspi_flash_read(address, 8,buf); //读取图片信息
		  image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
		  image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
		  image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
	#if 0   //打印图片信息地址
		  printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
	#endif
		  //printf("\r\n");
		  //printf("\r\n");
		  //printf("\r\n");
		  //memcpy((Image_Information_t *)image, (uint32_t *)address, 8);
		  //printf("address=%x\r\n",address);
		  length=image.Width*2;//获取图片大小
	
		  if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
		  {
			  length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
		  }
	
		
		  Address=image.Address;
		  //printf("Image Address=%d\r\n",Address);
		  //qspi_flash_read(Address,length,display_buf);

		  memcpy(LCD_Display_Buf,displaybuf2,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1


          for(y1=y,i=0;y1<y+image.Height/7;y1++,i++)
          {

			qspi_flash_read(Address+(num-1)*image.Width*image.Height*2/7  + i*image.Width*2,image.Width*2,Line_Display_Buf);

			


            for(int j=0;j<image.Width*2;j++)
            {
            	//printf("%d\r\n",j);
                //判断当前是否为纯黑

                  if(Line_Display_Buf[j]==0x00)
                  {
                	  //printf("Line_Display_Buf[j]==0x00\r\n");
                     //break;
				  }else
				  {
					  //printf("LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j]\r\n");
				       LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j];
				  }
           
				
			}
		  
		  	//qspi_flash_read(Address+num*image.Width*image.Height*2/10  + i*image.Width*2,image.Width*2,&LCD_Display_Buf[y1][x*2]);
		  }


          memcpy(displaybuf2,LCD_Display_Buf,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1

         










		  
		  //更新显示缓冲区  备份缓冲区
		  // memcpy(displaybuf1,display_buf,LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT); //将显示缓冲区2更新到主缓冲区1
}







void GUI_Show_Dialplate_Point(int x,int y,uint32_t address,uint8_t num)
{
 //printf("--------------%s-----------------\r\n",__FUNCTION__);
         uint8_t i=0;
         uint16_t  x1=0,y1=0;

		  uint32_t	length=0;
		  uint32_t Address=0;
		  Image_Information_t image;
		  uint8_t buf[8]={0};
		  qspi_flash_read(address, 8,buf); //读取图片信息
		  image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
		  image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
		  image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
	#if 0   //打印图片信息地址
		  printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
	#endif
		  length=image.Width*2;//获取图片大小
	
		  if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
		  {
			  length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
		  }
	
		
		  Address=image.Address;



		  memcpy(LCD_Display_Buf,displaybuf2,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1

          y=y-image.Height+num;
          x=x-num;
          for(y1=y,i=0;y1<y+image.Height;y1++,i++)
          {

			qspi_flash_read(Address + i*image.Width*2,image.Width*2,Line_Display_Buf);

			


            for(int j=0;j<image.Width*2;j++)
            {
            	//printf("%d\r\n",j);
                //判断当前是否为纯黑

                  if(Line_Display_Buf[j]==0x00)
                  {
                	  //printf("Line_Display_Buf[j]==0x00\r\n");
                     //break;
				  }else
				  {
					  //printf("LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j]\r\n");
				       LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j];
				  }
           
				
			}
		  
		  	//qspi_flash_read(Address+num*image.Width*image.Height*2/10  + i*image.Width*2,image.Width*2,&LCD_Display_Buf[y1][x*2]);
		  }


          memcpy(displaybuf2,LCD_Display_Buf,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1

      	  //更新显示缓冲区  备份缓冲区
		  // memcpy(displaybuf1,display_buf,LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT); //将显示缓冲区2更新到主缓冲区1
}



void GUI_Show_Dialplate_Pointer_Time(int x,int y,Dialplate_t *address,uint8_t num,uint8_t time)
{
   uint8_t i=0;
   uint16_t  x1=0,y1=0;

   //uint16_t j=0;
   
	uint32_t  length=0;
	uint32_t Address=0;
	Image_Information_t image;
	uint8_t buf[8]={0};  //保存图片的信息


	  if(time<=15)
	{
		
				qspi_flash_read(address[time%16], 8,buf); //读取图片信息
			  image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
			  image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
			  image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
			#if 0   //打印图片信息地址
			  printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
			#endif
			  length=image.Width*2;//获取图片大小
		
			  if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
			  {
				  length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
			  }
			  Address=image.Address;
			  memcpy(LCD_Display_Buf,displaybuf2,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1



			y=y-image.Height+num;
			 x=x-num;
			 for(y1=y,i=0;y1<y+image.Height;y1++,i++)
			 {
	
			   qspi_flash_read(Address + i*image.Width*2,image.Width*2,Line_Display_Buf);
	
			   
	
	
			   for(int j=0;j<image.Width*2;j++)
			   {
				   //printf("%d\r\n",j);
				   //判断当前是否为纯黑
					 if(Line_Display_Buf[j]==0x00)
					 {

					 }else
					 {
						 //printf("LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j]\r\n");
						  LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j];
					 }   
			   }
			   //qspi_flash_read(Address+num*image.Width*image.Height*2/10	+ i*image.Width*2,image.Width*2,&LCD_Display_Buf[y1][x*2]);
			 }	
			 memcpy(displaybuf2,LCD_Display_Buf,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1

	}
	else if(time<=30)
	{
	
		qspi_flash_read(address[15-time%16-1], 8,buf); //读取图片信息
	  	image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
	  	image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
	  	image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
#if 0	//打印图片信息地址
	  printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
#endif
	  length=image.Width*2;//获取图片大小
	
	  if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
	  {
		  length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
	  }
	  Address=image.Address;
	  memcpy(LCD_Display_Buf,displaybuf2,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1
	
	
	
	y=y-(num);//-image.Height+num;
	 x=x-num;
	 for(y1=y,i=image.Height;y1<y+image.Height;y1++,i--)
	 {
	
	   qspi_flash_read(Address + i*image.Width*2,image.Width*2,Line_Display_Buf);
	
	   
	
	
	   for(int j=0;j<image.Width*2;j++)
	   {
		   //printf("%d\r\n",j);
		   //判断当前是否为纯黑
			 if(Line_Display_Buf[j]==0x00)
			 {
	
			 }else
			 {
				 //printf("LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j]\r\n");
				  LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j];
			 }	 
	   }
	   //qspi_flash_read(Address+num*image.Width*image.Height*2/10	+ i*image.Width*2,image.Width*2,&LCD_Display_Buf[y1][x*2]);
	 }	
	 memcpy(displaybuf2,LCD_Display_Buf,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1



	}
	else if(time<45)
	{


		qspi_flash_read(address[15-time%16-1], 8,buf); //读取图片信息
	  	image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
	  	image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
	  	image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
#if 0	//打印图片信息地址
	  printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
#endif
	  length=image.Width*2;//获取图片大小

	  if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
	  {
		  length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
	  }
	  Address=image.Address;
	  memcpy(LCD_Display_Buf,displaybuf2,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1



	y=y-(num);//-image.Height+num;
	 x=x+num;
	 for(y1=y,i=image.Height;y1<y+image.Height;y1++,i--)
	 {

	   qspi_flash_read(Address + i*image.Width*2,image.Width*2,Line_Display_Buf);



#if 0
    for(int b=0;b< image.Width;b++)
    {
     	uint8_t temp_buf[2]={0};
	 
		temp_buf[0]=Line_Display_Buf[2*b];
		temp_buf[1]=Line_Display_Buf[2*b+1];

		Line_Display_Buf[2*b]  = Line_Display_Buf[image.Width*2 -  (2*b+1)]     ;
		Line_Display_Buf[2*b+1]= Line_Display_Buf[image.Width*2 -  (2*b)]   ;



	    Line_Display_Buf[image.Width*2 -  (2*b+1)]	=  temp_buf[0] ;
		Line_Display_Buf[image.Width*2 -  (2*b)]=	   temp_buf[1];

	}
#endif
     




	   for(int j=0;j<image.Width*2;j++)
	   {
		   //printf("%d\r\n",j);
		   //判断当前是否为纯黑
			 if(Line_Display_Buf[j]==0x00)
			 {

			 }else
			 {
				 //printf("LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j]\r\n");
				  LCD_Display_Buf[y1][(x)*2+j]=Line_Display_Buf[j];
			 }
	   }
	   //qspi_flash_read(Address+num*image.Width*image.Height*2/10	+ i*image.Width*2,image.Width*2,&LCD_Display_Buf[y1][x*2]);
	 }
	 memcpy(displaybuf2,LCD_Display_Buf,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1















	}
	else
	{


	}
  
#if 0

 //printf("--------------%s-----------------\r\n",__FUNCTION__);
         uint8_t i=0;
         uint16_t  x1=0,y1=0;

		  uint32_t	length=0;
		  uint32_t Address=0;
		  Image_Information_t image;
		  uint8_t buf[8]={0};
		  qspi_flash_read(address, 8,buf); //读取图片信息
		  image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
		  image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
		  image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
	#if 0   //打印图片信息地址
		  printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
	#endif
		  length=image.Width*2;//获取图片大小
	
		  if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
		  {
			  length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
		  }
	
		
		  Address=image.Address;



		  memcpy(LCD_Display_Buf,displaybuf2,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1

          y=y-image.Height+num;
          x=x-num;
          for(y1=y,i=0;y1<y+image.Height;y1++,i++)
          {

			qspi_flash_read(Address + i*image.Width*2,image.Width*2,Line_Display_Buf);

			


            for(int j=0;j<image.Width*2;j++)
            {
            	//printf("%d\r\n",j);
                //判断当前是否为纯黑

                  if(Line_Display_Buf[j]==0x00)
                  {
                	  //printf("Line_Display_Buf[j]==0x00\r\n");
                     //break;
				  }else
				  {
					  //printf("LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j]\r\n");
				       LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j];
				  }
           
				
			}
		  
		  	//qspi_flash_read(Address+num*image.Width*image.Height*2/10  + i*image.Width*2,image.Width*2,&LCD_Display_Buf[y1][x*2]);
		  }


          memcpy(displaybuf2,LCD_Display_Buf,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1

     #endif    










		  
		  //更新显示缓冲区  备份缓冲区
		  // memcpy(displaybuf1,display_buf,LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT); //将显示缓冲区2更新到主缓冲区1
}





void LCD_Set_Area_Display(unsigned int y0, unsigned int y1)
{
	      LCD_Write_Cmd(0x30);//命令
		  LCD_Write_Data((y0 >> 8)&0xFF);//命令
		  LCD_Write_Data(y0 & 0xFF);//命令
		  LCD_Write_Data((y1 >> 8)&0xFF);//命令
		  LCD_Write_Data(y1 & 0xFF);//命令
		  Part_Display_Mode_ON();//部分显示模式开启
#if 0
	if((y1-y0) < LCD_GUI_HEIGHT)
	{

	  LCD_Write_Cmd(0x30);//命令

	
	 

	  
	  LCD_Write_Data((y0 >> 8)&0xFF);//命令
	  LCD_Write_Data(y0 & 0xFF);//命令
	  LCD_Write_Data((y1 >> 8)&0xFF);//命令
	  LCD_Write_Data(y1 & 0xFF);//命令

	  Part_Display_Mode_ON();//部分显示模式开启

#if 0
	  
		write_cmd(0x30);
		write_data((y0 >> 8)&0xFF);
		write_data(y0 & 0xFF);
		write_data((y1 >> 8)&0xFF);
		write_data(y1 & 0xFF);

#endif
	}
	else
	{
		Part_Display_Mode_OFF();//部分模式关闭
	}

	#endif

}

//部分显示模式开启
void Part_Display_Mode_ON()

{


   LCD_Write_Cmd(0x12);
   
   //LCD_Write_Data(uint8_t data);


  // LCDSPI_InitCMD_test(0x12, 0);//命令

   //write_cmd(0x12);
}
//部分显示模式关闭
void Part_Display_Mode_OFF()
{
   LCD_Write_Cmd(0x13);

   //write_cmd(0x13); //normal mode.
}



void LCD_RAM_Write(uint16_t xs,uint16_t xe,uint16_t ys,uint16_t ye)
{

	//XS=0 (0h) YS=0 (0h) and XE=239 (Efh), YE=319 (13Fh).
	//地址设置不能超过 width 240  height 320


}
//LCD垂直滚动模式显示
void LCD_Vertical_Scroll_Mode_Display()
{
#if 1
	uint8_t data;
	uint16_t tfa=20, vsa=280+20,  bta=320-vsa-tfa;

	//垂直滚动只有一种类型，是由命令“垂直滚动”决定的
  //“定义”(33h)和“垂直滚动起始地址”(37h)
  //当垂直滚动定义参数(TFA+VSA+BFA) =320时。
	//TFA为顶部固定显示区域，这部分指定后会固定显示，不会滚动显示；
	//VSA为滚动区域，这部分指定后会滚动显示；BFA为底部固定显示区域，这部分指定后会固定显示，不会滚动显示。
	//因为整个显存垂直有320行像素点，显然一个字节8位是装不下的。
	//所以「TFA、VSA、BFA三个区域的值设置都是16位，并且三个值加起来要等于320，否则滚动区域定义失败」。
	//这里我设置整屏都滚动，所以TFA和BFA的值都设置为0，VSA的值设为320。

    LCD_Write_Cmd(0x33);//发送滚动命令
	  data = tfa >> 8;
    LCD_Write_Data(data);
    data = tfa;
    LCD_Write_Data(data);
    data = vsa >> 8;
    LCD_Write_Data(data);
    data = vsa;
    LCD_Write_Data(data);

	
    data = bta >> 8;
    LCD_Write_Data(data);
    data = bta;
    LCD_Write_Data(data);
    
#endif
}



//LCD垂直滚动模式显示
void LCD_Vertical_Scroll_Mode_Exit()
{
    LCD_Write_Cmd(0x28);//发送滚动命令
	
	LCD_Write_Cmd(0x29);//发送滚动命令


}



void LCD_Set_Address(uint16_t i)
{


    LCD_Write_Cmd(0x37);//发送滚动命令
    LCD_Write_Data((i>> 8)&0xFF);
    LCD_Write_Data(i & 0xFF);

#if 0
  write_cmd(0x37);    //发送滚动起始地址
  write_data(i / 256);//设置地址
  write_data(i % 256);//设置地址
#endif
}






void GUI_Hardware_Scroll()
{






}




Is_Alpha_t Image_Is_Alpha(uint32_t Address)
{

      uint8_t alpha=0;
	  uint32_t  length=0;
	  uint32_t address=Address;
	  Image_Information_t image;
	  uint8_t buf[9]={0};
	  qspi_flash_read(address+9, 1,&alpha); //读取图片信息

	  if(alpha==0x01)
	     return Alpha_true;
	  	else
		 return Alpha_false;
	 
}



#define UI_DEV_255(_x)   (((  (_x)+128)*257)>>16)

/*   
		函数功能:		
		参数  src     底色
		参数pixel     颜色
		参数alpha     alpha透明
*/
static unsigned short Pixel_Alpha_Blend(unsigned short src,unsigned short pixel,unsigned alpha)
{
     unsigned short r,g,b,r_b,g_b,b_b;

    if(alpha ==0xff)
    {
       return pixel;
	}else if (alpha==0)
	{
	  return src;
	}else if (src==0 ){

	
	}else{

	  r=((pixel>>5)&0x00f8);
	  g=((pixel<<5)&0x00e0)+((pixel>>11) &0x001c);
	  b=((pixel)&0x00f8);

	  r_b=((src>>5)&0x00f8);
	  g_b=((src<<5)&0x00e0) + ((src>>11) &0x001c);
	  b_b=((src)&0x00f8);


    r= (    UI_DEV_255(r_b*(0xff-alpha))  +r)&0x00f8;

	g= (	UI_DEV_255(g_b*(0xff-alpha))  +g)&0x00fc;

	b= (    UI_DEV_255(b_b*(0xff-alpha))  +b)&0x00f8;

	  

      pixel =(r<<5)+(g>>5)+(g<<11)+b;
	}
	return pixel;
}



void GUI_Show_Image_Alpha(uint16_t x,uint16_t y,uint32_t address)
{
 //printf("--------------%s-----------------\r\n",__FUNCTION__);

          uint8_t temp_buf[240*3]={0};
          uint8_t i=0;
		  uint32_t flag=0;
          uint16_t  x1=0,y1=0;
          uint16_t color=0;

		  uint32_t	length=0;
		  uint32_t Address=0;
		  Image_Information_t image;
		  uint8_t buf[9]={0};
		  qspi_flash_read(address, 9,buf); //读取图片信息
		  image.Width= (buf[0]<<8)|buf[1]; //获取图片宽度
		  image.Height=(buf[2]<<8)|buf[3]; //获取图片高度
		  image.Address=((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|(buf[7]));//获取图片地址
		  image.Alpha=buf[8];


     	  if(image.Alpha==0x01)
     	  {

			printf("Have Alpha\r\n");

		   	length=image.Width*3;//获取图片大小
	
		  if(image.Width*image.Height*3>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
		  {
			  length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
			
		  }
			
			

		  }
		  	else
		  {

		    printf("No Have Alpha\r\n");
		 		 length=image.Width*2;//获取图片大小
		  
			if(image.Width*image.Height*2>=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2)
			{
				length=LCD_GUI_WIDTH*LCD_GUI_HEIGHT*2;
			}
			  
		  }



#if 1	//打印图片信息地址
			printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x,image.Alpha=%d\r\n",image.Width,image.Height,image.Address,image.Alpha);
#endif

		  
	#if 0   //打印图片信息地址
		  printf("image.Width=%d\r\n,image.Height=%d\r\n,image.Address=%x\r\n",image.Width,image.Height,image.Address);
	#endif
		  Address=image.Address;
		  memcpy(LCD_Display_Buf,displaybuf2,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1

          for(y1=y,i=0;y1<y+image.Height;y1++,i++)
          {

            memcpy(Line_Display_Buf,&LCD_Display_Buf[y1],LCD_GUI_WIDTH*2);
			qspi_flash_read( Address + i * image.Width * 3 ,image.Width*3,temp_buf);
			
            for(int j=0;j<image.Width;j++)
            {
            	//printf("%d\r\n",j);
                //判断当前是否为纯黑
                  if(temp_buf[3*j+2]==0x00)
                  {
                	//printf("Line_Display_Buf[j]==0x00\r\n");
                    //break;
					flag+=2;
				  }else				  
				  {
				  #if 1
					//颜色混合
				   color = Pixel_Alpha_Blend( (Line_Display_Buf[(x*2 -1)+flag]<<8) | Line_Display_Buf[(x*2 -1)+flag  +1],
					  (temp_buf[j*3+1]<<8)| temp_buf[j*3+0],temp_buf[3*j+2]);

					Line_Display_Buf[(x*2 -1)+flag] 		   =  color>>8;
					Line_Display_Buf[(x*2 -1)+flag	+1] 	   =  color & 0xff;


                   #else
                    Line_Display_Buf[(x*2 -1)+flag]	           =  temp_buf[j*3+1];
			        Line_Display_Buf[(x*2 -1)+flag  +1]        =  temp_buf[j*3+0];
					#endif
				    flag+=2;
					//printf("LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j]\r\n");
				    //LCD_Display_Buf[y1][x*2+j]=Line_Display_Buf[j];
				  }		
			}
			flag=0;
			 memcpy(&LCD_Display_Buf[y1],Line_Display_Buf,LCD_GUI_WIDTH*2);
		  
		  	//qspi_flash_read(Address+num*image.Width*image.Height*2/10  + i*image.Width*2,image.Width*2,&LCD_Display_Buf[y1][x*2]);
		  }
          memcpy(displaybuf2,LCD_Display_Buf,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1
		  //更新显示缓冲区  备份缓冲区
		  // memcpy(displaybuf1,display_buf,LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT); //将显示缓冲区2更新到主缓冲区1
}














